<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

/**
 * 新闻内容管理
 */
class NewsContent extends Backend
{

    protected $model = null;
    protected $category = null;

    protected $with = ['imgFile', 'reImgFile','newsClass'];

    protected $beforeActionList = [
        'loadingCategory'
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\NewsContent();
        $this->category = new \app\admin\model\NewsClass();
    }

    public function index()
    {
        //list($where, $sort, $order, $offset, $limit) = $this->buildparams();
		list($where, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if (isset($params['query']) && $params['query'] != '') {
            $where['title'] = ['like', '%' . $params['query'] . '%'];
        }
        if(isset($params['type_id']) && $params['type_id'] != ''){
            $where['type_id'] = $params['type_id'];
        }
        $list = $this->model
            ->with($this->with ? $this->with : null)
            ->where($where)
            //->order($sort, $order)
			->order('sort desc,create_time desc')
            ->paginate($limit);
        $this->assign('list', $list);
        $this->assign('params', $params);
        return $this->fetch();
    }

    protected function loadingCategory()
    {
        $this->assign('new_classes', $this->category->select());
    }

}
